package org.hwadee.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.hwadee.entity.Message;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MessageMapper {

    @Select("select * from message where messageId=#{messageId}")
    Message getMessageById(Integer messageId);

    @Select("select * from message where sender=#{senderId}")
    List<Message> getMessageBySenderId(Integer senderId);

    @Select("select * from message where receiver=#{receiverId}")
    List<Message> getMessageByReceiverId(Integer receiverId);

    @Select("select * from message where (receiver=#{userIdA} and sender=#{userIdB}) or (receiver=#{userIdB} and sender=#{userIdA}) order by sendTime desc limit 1")
    Message getNewestMessageByUserId(@Param("userIdA") Integer userIdA, @Param("userIdB") Integer userIdB);

    @Insert("insert into message (sender, receiver, messageContent, sendTime) values (#{sender}, #{receiver}, #{messageContent}, #{sendTime})")
    void insertMessage(Message message);

    @Select("select * from message where (sender=#{userIdA} and receiver=#{userIdB}) or (receiver = #{userIdA} and sender=#{userIdB}) order by sendTime asc")
    List<Message> listMessagesByUserId(@Param("userIdA") Integer userIdA, @Param("userIdB") Integer userIdB);
}
